<template>
  <p-drawer
    placement="right"
    :closable="false"
    width="calc(100vw - 200px)"
    :visible="visible"
    :mask-closable="false"
    :destroy-on-close="true"
    :z-index="1000"
    @close="onClose"
  >
    <TaskTitle icon-name="Det" title-name="客户详情" color="green" />
    <p-button type="primary" class="btn" @click="getDrawer">
      编辑
    </p-button>
    <div v-if="getFormData && formData.code" class="list-container">
      <p-tabs
        :animated="false"
        class="tabs-container"
        default-active-key="1"
        @change="tabsChange"
      >
        <p-tab-pane key="1" tab="基本信息">
          <div class="list-main">
            <CustomerDetail :detail="formData" />
          </div>
        </p-tab-pane>
        <p-tab-pane key="2" tab="业务线">
          <TableBusinessLine
            ref="businessLines"
            :key-tabs="keyTabs"
            :code="formData.code"
            :show-btn="false"
            @change="closeCallback"
          />
        </p-tab-pane>
        <p-tab-pane key="3" tab="联系人">
          <TabsContacts
            :code="formData.code"
            :key-tabs="keyTabs"
            :name="formData.name"
            :show-btn="false"
            :show-detail="true"
            :show-create="true"
          />
        </p-tab-pane>
        <p-tab-pane key="5" tab="法人">
          <TabsLegalPerson
            :code="formData.code"
            :key-tabs="keyTabs"
            :show-btn="false"
          />
        </p-tab-pane>
        <p-tab-pane key="4" tab="地址">
          <TabsAddress
            :code="formData.code"
            :key-tabs="keyTabs"
            :show-btn="false"
            :show-detail="true"
          />
        </p-tab-pane>
        <p-tab-pane key="9" tab="合同">
          <TabsContract :code="formData.code" :key-tabs="keyTabs" :show-btn="false" />
        </p-tab-pane>
        <p-tab-pane key="8" tab="决策关系">
          <div
            style="width: 100%; height: 200px; font-size: 20px; text-align: center"
          >
            该页面待开发
          </div>
        </p-tab-pane>
        <p-tab-pane key="10" tab="线索">
          <TabsClue :code="formData.code" :key-tabs="keyTabs" :show-btn="false" />
        </p-tab-pane>
        <p-tab-pane key="11" tab="商机">
          <TabsBusiness :code="formData.code" :key-tabs="keyTabs" :show-btn="false" />
        </p-tab-pane>
        <p-tab-pane key="12" tab="拜访">
          <TabsVisit :code="formData.code" :key-tabs="keyTabs" :show-btn="false" />
        </p-tab-pane>
        <p-tab-pane key="6" tab="附件">
          <TabsEnclosure
            :code="formData.code"
            :key-tabs="keyTabs"
            :show-btn="false"
          />
        </p-tab-pane>
        <p-tab-pane key="7" tab="操作日志">
          <ProgressPortrait :operation-log="formData.logs" />
        </p-tab-pane>
      </p-tabs>
    </div>
    <ButtonGroup
      class="footer"
      :options="footerButtons"
      @click="handleButtonClick"
    />
  </p-drawer>
</template>

<script>
import {
  getCustomerMainDetail,
} from '@/api/customerMaintain.js';
import DrawerMixin from './mixin';
import TableBusinessLine from '@/components/task/TableBusinessLine.vue';
import TabsEnclosure from './TabsEnclosure.vue';
import TabsAddress from './TabsAddress.vue';
import TabsLegalPerson from './TabsLegalPerson.vue';
import TabsContacts from './TabsContacts.vue';
import ProgressPortrait from '@/components/task/ProgressPortrait.vue'
import CustomerDetail from '@/views/customer-creation/components/CustomerDetail.vue'
import TabsContract from './TabsContract.vue'
import TabsClue from './TabsClue.vue'
import TabsBusiness from './TabsBusiness.vue'
import TabsVisit from './TabsVisit.vue'

export default {
  components: {
    TableBusinessLine,
    TabsEnclosure,
    TabsAddress,
    TabsLegalPerson,
    TabsContacts,
    ProgressPortrait,
    CustomerDetail,
    TabsContract,
    TabsClue,
    TabsBusiness,
    TabsVisit
  },
  mixins: [DrawerMixin],
  props: {
    priceId: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      showBottom: true,
      formData: {},
      keyTabs: '1',
    };
  },

  computed: {
    getFormData() {
      return Object.keys(this.formData).length || 0;
    },
    footerButtons() {
      return [
        {
          key: 'close',
          label: '返回',
        },
      ];
    },
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.init();
        }
      },
      immediate: true,
    },
    keyTabs: {
      handler(newVal) {
        if (newVal === '1' || newVal === '7') {
          this.init();
        }
      },
      immediate: true,
    },
  },
  created() {
    // this.init()
  },
  methods: {
    init() {
      if (!this.priceId) return;
      this.getbusinessFormInfo();
    },
    async getbusinessFormInfo() {
      const res = await getCustomerMainDetail(this.priceId);
      this.formData = res?.data?.data || {};
      // console.log(this.formData, '12we');
    },
    async tabsChange(key) {
      this.keyTabs = key;
      console.log(key);
    },
    onClose() {
      this.$emit('update:visible', false);
      this.$emit('fresh');
    },
    // 返回
    closeCallback() {
      this.keyTabs = '1';
      this.onClose();
    },
    handleButtonClick(key) {
      const fn = `${key}Callback`;
      this[fn] && this[fn]();
    },
    // 编辑潜客
    getDrawer() {
      this.$emit('showEdit', this.priceId);
      this.closeCallback()
    },
  },
};
</script>

<style lang="less" scoped>
.footer {
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 1px solid #e5e6e9;
}
.list-container {
  height: calc(100% - 110px);
  display: flex;
  flex-direction: column;
  overflow-y: scroll;
  position: relative;
  box-sizing: border-box;
  padding: 10px 24px;
}
.list-main {
  height: calc(100% - 400px);
  overflow-y: scroll;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  padding: 10px 24px;
}
/deep/ .poros-drawer-body {
  min-width: 1136px;
  height: 100%;
  padding: 0;
}
.progress-container {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.tabs-container {
  height: 100% !important;
  overflow-y: scroll;
}
.btn {
  position: absolute;
  right: 30px;
  top: 7px;
  z-index: 101;
}
</style>
